﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace linkouqs.Gamekit {

    public class CharacterStatePush : CharacterState {

        CharacterAbilityPush m_Push;

        float m_LastX;

        public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
            base.OnStateEnter(animator, stateInfo, layerIndex);

            m_Push = animator.GetComponent<CharacterAbilityPush>();
            m_LastX = Character.transform.position.x;
        }

        public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
            base.OnStateUpdate(animator, stateInfo, layerIndex);

            if (!Character.IsGround) {
                m_Push.StopAction();
                return;
            }

            if (m_Push.PushableObject == null) {
                m_Push.StopAction();
                return;
            }

            if (Character.IsPressedAction()) {

                /// Pull

                float x = Character.transform.position.x - m_LastX;

                    m_Push.PushableObject.transform.position += new Vector3(x, 0);

                Character.LockDirection = true;

            } else {

                /// Push

                if (!Character.IsFaceTo(m_Push.PushableObject.transform)) {
                    m_Push.StopAction();
                    return;
                }

                if (!Character.IsPressedRun()) {
                    m_Push.StopAction();
                    return;
                }

                Character.LockDirection = false;

            }

            m_LastX = Character.transform.position.x;
        }

        public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
            m_Push = null;
            Character.LockDirection = false;

            base.OnStateExit(animator, stateInfo, layerIndex);
        }

    }

}
